class SearchImageModel {
  final String imageUrl;
  final int index;

  SearchImageModel({
    required this.imageUrl,
    required this.index,
  });

  factory SearchImageModel.fromJson(String imageUrl, int index) {
    // 清理URL字符串，移除多余的空格和反引号
    String cleanUrl = imageUrl.trim().replaceAll('`', '').replaceAll(' ', '');
    
    return SearchImageModel(
      imageUrl: cleanUrl,
      index: index,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'image_url': imageUrl,
      'index': index,
    };
  }
}

class SearchResponse {
  final int code;
  final int page;
  final int maxPage;
  final int count;
  final List<SearchImageModel> images;
  final String td;

  SearchResponse({
    required this.code,
    required this.page,
    required this.maxPage,
    required this.count,
    required this.images,
    required this.td,
  });

  factory SearchResponse.fromJson(Map<String, dynamic> json) {
    List<String> resUrls = List<String>.from(json['res'] ?? []);
    List<SearchImageModel> imageList = [];
    
    for (int i = 0; i < resUrls.length; i++) {
      imageList.add(SearchImageModel.fromJson(resUrls[i], i));
    }
    
    return SearchResponse(
      code: json['code'] ?? 200,
      page: int.tryParse(json['page'].toString()) ?? 1,
      maxPage: json['maxpage'] ?? 1,
      count: json['count'] ?? 0,
      images: imageList,
      td: json['td']?.toString() ?? '1',
    );
  }

  bool get hasMore => page < maxPage;
  bool get isSuccess => code == 200;
}